package com.example;

import java.io.UnsupportedEncodingException;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {
	
	private String name = "n/a";
	
	public MyResource(@QueryParam("name") String name) {
		this.name = name;
	}

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it! This is jersey-simple-webapp";
    }
    
    @GET @Path("listname")
    @Produces(MediaType.TEXT_PLAIN)
    public String nameIt(@DefaultValue("ah Pooh") @QueryParam("name") String name) throws UnsupportedEncodingException {
//    	return "Welcome " + new String(name.getBytes("ISO-8859-1"));
    	return "Welcome " + name + "\n" + "Constructor : " + this.name;
    }
    
 // This method is called if TEXT_PLAIN is request
    @GET @Path("helloworld")
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
      return "Hello Jersey";
    }

    // This method is called if XML is request
    @GET @Path("helloworld")
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
      return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
    }

    // This method is called if HTML is request
    @GET @Path("helloworld")
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
      return "<html> " + "<title>" + "Hello Jersey" + "</title>"
          + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
    }
    
    @GET @Path("getjaxb")
    @Produces(MediaType.APPLICATION_XML)
    public Planet sayJAXB() {
    	Planet p = new Planet();
    	p.id = 10;
    	p.name = "testPlanet";
    	p.radius = 12.3;
    	
    	return p;
    }
    
}
